from rest_framework.views import APIView
from django.utils import timezone
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser

from users.models import User
from goods.models import GoodsVisitCount
from meiduo_admin.serializers.statistical import GoodsVisitSerializer


#  GET/meiduo_admin/statistical/total_count/
class UserTotalCountView(APIView):
    """用户总统计"""
    # 权限设置 是管理用户
    permission_classes = [IsAdminUser]

    def get(self,request):

        count = User.objects.count()

        now_data = timezone.localtime()

        data = {
            'count':count,
            'now_data':now_data
        }

        return Response(data)


#  GET /meiduo_admin / statistical / day_increment /
class UserDayCountView(APIView):
    """日增用户"""

    # 权限设置 是管理用户
    permission_classes = [IsAdminUser]

    def get(self,request):

        now_date= timezone.localtime().replace(hour=0,minute=0,second=0,microsecond=0)
        # dete_joined 用户注册时间
        count = User.objects.filter(date_joined__gte=now_date).count()

        data = {
            'count': count,
            'date': now_date.date()
        }

        return Response(data)


# GET /meiduo_admin/statistical/day_active/
class UserActiveCountView(APIView):
    """当日用户活跃量"""
    # 权限设置 是管理用户
    permission_classes = [IsAdminUser]

    def get(self,request):

        now_date = timezone.localtime().replace(hour=0,minute=0,second=0,microsecond=0)
        # last_login  最后登陆时间
        count = User.objects.filter(last_login__gte=now_date).count()

        response_data = {
            'count':count,
            'now_date':now_date.date()
            }

        return Response(response_data)


# GET /meiduo_admin/statistical/day_orders/
class UserOrderCountView(APIView):
    """当日用户下单量"""
    # 权限设置 是管理用户
    permission_classes = [IsAdminUser]

    def get(self,request):

        now_date = timezone.localtime().replace(hour=0,minute=0,second=0,microsecond=0)

        user = User.objects.get(id=1)
        # order__create_time 下单时间
        count = User.objects.filter(orders__create_time__gte=now_date)

        response_data = {
            'count':count,
            'noe_date':now_date.date()
        }

        return Response(response_data)


# GET /meiduo_admin/statistical/month_increment/
class UserMonthCountView(APIView):
    """统计30天注册用户"""
    # 权限设置 是管理用户
    permission_classes = [IsAdminUser]

    def get(self,request):

        now_date = timezone.localtime().replace(hour=0,minute=0,second=0,microsecond=0)
        # 起始时间
        begin_date = now_date - timezone.timedelta(days=29)

        count_list =[]

        for i in range(30):

            # 当天时间和下一天时间
            cur_date = begin_date + timezone.timedelta(days=i)

            next_date = cur_date + timezone.timedelta(days=1)
            # date_joined 注册时间
            count = User.objects.filter(date_joined__gte=cur_date,date_joined__lt=next_date).count()

            count_list.append({'date': cur_date.date(),
                                'count': count})

        return Response(count_list)


# GET /meiduo_admin/statistical/goods_day_views/
class GoodsDayView(APIView):
    """日商品访问量"""
    # 权限设置 是管理用户
    permission_classes = [IsAdminUser]

    def get(self,request):

        now_date = timezone.now()

        goods_list = GoodsVisitCount.objects.get(data=now_date)

        serializer = GoodsVisitSerializer(goods_list, many=True)

        serializer.is_valid()

        return Response(serializer.validated_data)

